using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using SoinusLibrary.Data;

/// <summary>
/// Summary description for SoinusDBManager
/// </summary>
/// 
namespace SoinusLibrary.Data
{
    public class SoinusDBManager : DBManager, ILogin
    {
        private static SoinusDBManager instance;
        public static SoinusDBManager getInstance()
        {
            if (instance == null) instance = new SoinusDBManager();

            return instance;
        }

        private SoinusDBManager()
            : base(ConfigurationManager.ConnectionStrings["SoinusDBConnectionString"].ConnectionString)
        {
        }

        public bool RegistryUser(string email, string password)
        {
            Int32 rowsAffected = -1;
            SqlCommand cmd = null;

            try
            {
                this.Connect();

                cmd = new SqlCommand("RegistryUser", this.con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@email", SqlDbType.NVarChar).Value = email;
                cmd.Parameters.Add("@password", SqlDbType.NVarChar).Value = password;
                cmd.Parameters.Add("@rowsAffected", SqlDbType.Int).Value = 0;
                cmd.Parameters["@rowsAffected"].Direction = ParameterDirection.Output;
                cmd.ExecuteNonQuery();
                rowsAffected = (Int32)cmd.Parameters["@rowsAffected"].Value;
                cmd.Dispose();

                this.Disconnect();
            }
            catch (SqlException ex)
            {
                try
                {
                    Log.LogManager.Instance.WriteLog(new Log.LogEntry(ex.Source, ex.Message));
                }
                catch (Exception exLog)
                {
                    System.Console.WriteLine("%1 %2", exLog.StackTrace, exLog.Message);
                }
            }

            if (rowsAffected == 1)
                return true;
            else return false;
        }

        public bool Authenticate(string email, string password)
        {
            Int32 rowsAffected = -1;
            SqlCommand cmd = null;

            this.Connect();

            cmd = new SqlCommand("Authenticate", this.con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@email", SqlDbType.NVarChar).Value = email;
            cmd.Parameters.Add("@password", SqlDbType.NVarChar).Value = password;
            rowsAffected = (Int32) cmd.ExecuteScalar();
            cmd.Dispose();

            this.Disconnect();

            if (rowsAffected == 1)
                return true;
            else return false;
        }

        public bool UserExists(string email)
        {
            Int32 rowsAffected = -1;
            SqlCommand cmd = null;

            this.Connect();

            cmd = new SqlCommand("UserExists", this.con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@email", SqlDbType.NVarChar).Value = email;
            rowsAffected = (Int32)cmd.ExecuteScalar();
            cmd.Dispose();

            this.Disconnect();

            if (rowsAffected == 1)
                return true;
            else return false;
        }
    }
}
